using FreeRedis;
using FytSoa.Common.Utils;
using Newtonsoft.Json;

namespace FytSoa.Domain.Core.Cache
{
    public class RedisService
    {
        public static RedisClient cli => new RedisClient(AppConfigUtils.Configuration[KeyUtils.REDISLOCALHOST]);
        
        public static readonly RedisService Instance;
        static RedisService()
        {
            Instance = new RedisService();
        }

        /// <summary>
        /// 查询Redis信息
        /// </summary>
        /// <param name="redisKey"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Get<T>(string redisKey) where T : new()
        {
            var redisStr = cli.Get(redisKey);
            return !string.IsNullOrEmpty(redisStr) ? JsonConvert.DeserializeObject<T>(redisKey) : new T();
        }

        /// <summary>
        /// 查询Redis信息
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public void SetJson<T>(string redisKey,T t)
        {
            cli.Set(redisKey,JsonConvert.SerializeObject(t));
        } 
    }
}